package com.hckjsec.common.aspose;

import com.documents4j.api.DocumentType;
import com.documents4j.api.IConverter;
import com.documents4j.job.LocalConverter;
import com.hckjsec.common.utils.DateUtils;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;

import java.io.*;

public class PdfUtil {
    static final int wdDoNotSaveChanges = 0;// 不保存待定的更改。
    static final int wdFormatPDF = 17;// PDF 格式

    /**
     　　* word转pdf
     * @param wordFilePath word文件路径
     * @param pdfFilePath  pdf文件路径
     * @return 成功或失败
     */
    public static boolean docxToPdf(String wordFilePath, String pdfFilePath) {
        boolean result = false;
        long start = System.currentTimeMillis();
        File inputFile = new File(wordFilePath);
        File outputFile = new File(pdfFilePath);
        try {
            InputStream inputStream = new FileInputStream(inputFile);
            OutputStream outputStream = new FileOutputStream(outputFile);
            IConverter converter = LocalConverter.builder().build();
            converter.convert(inputStream).as(DocumentType.DOCX).to(outputStream).as(DocumentType.PDF).execute();
            outputStream.close();
            result = true;
        } catch (Exception e) {

            e.printStackTrace();
        }
        long end = System.currentTimeMillis();
        System.out.println("转换完成1..用时：" + (end - start) + "ms.");
        return result;
    }





    public static void wordToPdf(String wordpath, String pdfpath) {

        System.out.println("启动Word...");
        long start = System.currentTimeMillis();
        ActiveXComponent app = null;
        try {
            //打开word应用程序
            app = new ActiveXComponent("Word.Application");
            app.setProperty("Visible", false);
            //获得文档集合，用来操作我们需要处理的文档.
            Dispatch docs = app.getProperty("Documents").toDispatch();
            //System.out.println("打开文档..." + wordpath);
            //打开word文档
            Dispatch doc = Dispatch.call(docs,//
                    "Open", //
                    wordpath,// FileName
                    false,// ConfirmConversions
                    true // ReadOnly
            ).toDispatch();
            //System.out.println("转换文档到PDF..." + pdfpath);
            File tofile = new File(pdfpath);
            //创建存放pdf的文件夹
//            if (tofile.exists()) {
//                tofile.delete();
//            }
            //将word另存为pdf
            Dispatch.call(doc,//
                    "SaveAs", //
                    pdfpath, // FileName
                    wdFormatPDF);
            //关闭word文档
            Dispatch.call(doc, "Close", false);
            long end = System.currentTimeMillis();
            System.out.println("转换完成..用时：" + (end - start) + "ms.");
        } catch (Exception e) {
            System.out.println("========Error:文档转换失败：" + e.getMessage());
        } finally {
            if (app != null)
                app.invoke("Quit", wdDoNotSaveChanges);
        }
    }





    public static void main(String[] args) {
        String wordPath = "D:\\11\\11.doc";
        String pdfPath = "D:\\11\\11.pdf";
        wordToPdf(wordPath,pdfPath);
        String wordPath1 = "D:\\11\\22.doc";
        String pdfPath1 = "D:\\11\\22.pdf";
        docxToPdf(wordPath1,pdfPath1);
    }
}
